package com.dk.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.dk.service.SmsService;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 发送短信
 */
@Service
public class SmsServiceImpl implements SmsService {
    // 设置鉴权参数，初始化客户端
    private DefaultProfile profile = DefaultProfile.getProfile(
            "cn-qingdao",// 地域ID
            "LTAI4Fz7e5aqwbXHSqS7grS8",// 您的AccessKey ID
            "qsIhqBLuWDiSiOpcEb8vU6l3Bc7i8z");// 您的AccessKey Secret
    private IAcsClient client = new DefaultAcsClient(profile);

    private static void log_print(String functionName, Object result) {
        Gson gson = new Gson();
        System.out.println("-------------------------------" + functionName + "-------------------------------");
        System.out.println(gson.toJson(result));
    }

    /**
     * 发送短信
     */
    @Override
    public void sendSms(String code,String phone) throws ClientException {
        CommonRequest request = new CommonRequest();
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        // 接收短信的手机号码
        request.putQueryParameter("PhoneNumbers", phone);
        // 短信签名名称。请在控制台签名管理页面签名名称一列查看（必须是已添加、并通过审核的短信签名）。
        request.putQueryParameter("SignName", "DYN商城");
        // 短信模板ID
        request.putQueryParameter("TemplateCode", "SMS_194061587");
        // 短信模板变量对应的实际值，JSON格式。
        request.putQueryParameter("TemplateParam", "{\"code\":"+code+"}");
        CommonResponse commonResponse = client.getCommonResponse(request);
        String data = commonResponse.getData();
        String sData = data.replaceAll("'\'", "");
        log_print("sendSms", sData);
        Gson gson = new Gson();
        Map map = gson.fromJson(sData, Map.class);
        Object bizId = map.get("BizId");
        System.out.println("BizId" + bizId);
    }


    @Override
    public void sendSmsByApl(String code, String phone) {
        
    }
}
